package samsung.sds.resourceManagement.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import samsung.sds.resourceManagement.model.ResourceEntity;
import samsung.sds.resourceManagement.model.ResourceToRole;
import samsung.sds.resourceManagement.service.ResourceManagementService;
import samsung.sds.roleManagement.model.RoleManagementEntity;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
/**
 * @author pt.fang
 * @createDate 2016-01-15
 * @modifier by fei3.liu
 * @modificationDate 2016-02-22
 * 组织机构管理控制器
 */
@Controller
@RequestMapping("/resourceManagement")
public class ResourceManagementController {

	@Autowired
	private ResourceManagementService resourceManagementServiceImpl;

	@Autowired		
    private RequestMappingHandlerMapping requestMappingHandlerMapping; 

	/**
	 * 跳转到资源页面
	 */
	@RequestMapping(value="/resourceList",method=RequestMethod.POST)
	public String ResourceList(HttpServletRequest request,ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return "/resourceManagement/resourceList";
	}

	/**
	 * 分页查询列表
	 * @param request
	 * @return PageData
	 */
	@RequestMapping(value="/listPageData",method=RequestMethod.POST)
	@ResponseBody
	public PageData<ResourceEntity> getList(HttpServletRequest request) {
			QueryParam param = CommonTools.getQueryParam(request);
			return resourceManagementServiceImpl.queryListPage(param);
	}
	
	/**
	 * 获取资源实例和角色实例，跳转到添加/修改页面
	 * @param modelMap
	 * @param id
	 * @return String
	 */
	@RequestMapping(value = "/resourceDetails", method = RequestMethod.POST)
	public String resourceDetails(ModelMap modelMap, String id){
		//得到除已有角色之外的所有角色集合
		List<RoleManagementEntity> roleList = resourceManagementServiceImpl.getResToRoleList(id);
		modelMap.addAttribute("roleList", roleList);
		if (id != null && id != "") {
			//通过资源ID得到资源信息
			ResourceEntity resource = resourceManagementServiceImpl.findEntityById(id);
			modelMap.addAttribute("resource", resource);
			//通过资源ID得到角色集合
			List<ResourceToRole> roleArray = resourceManagementServiceImpl.getRoleArray(id);
			modelMap.addAttribute("roleArray", roleArray);
		}
		return "/resourceManagement/addOrUpdateResource";
	}

	/**
	 * 验证资源地址唯一性
	 * @param resUrl
	 * @param optype
	 * @param res_url
	 * @return boolean
	 * @throws IOException
	 */
	@RequestMapping(value="/checkResource",method=RequestMethod.POST)
	@ResponseBody
	public boolean checkResource(String resUrl,String optype, String res_url) throws IOException {
		return resourceManagementServiceImpl.checkResource(resUrl, optype, res_url);
	}

	/**
	 * 添加/修改资源
	 * @param resource
	 * @return String
	 */
	@RequestMapping(value="/addOrUpdateResouce",method=RequestMethod.POST)
	@ResponseBody
	public String addOrUpdateResouce(String optype,ResourceEntity resource) {
		// 参数验证
		if (resource == null || optype == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		// 新增
		if (optype.equalsIgnoreCase("create")) {
			int rlt = resourceManagementServiceImpl.insertEntity(resource);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
		// 修改
		} else if ("update".equalsIgnoreCase(optype)) {
			int rlt = resourceManagementServiceImpl.updateEntity(resource);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
		} else {
			return Constants.ERRMSG_PARAM_ERROR;
		}
	}

	/**
	 * 删除资源
	 * @param id
	 * @return String
	 */
	@RequestMapping(value="/deleteResource",method=RequestMethod.POST)
	@ResponseBody
	public String deleteResource(@RequestBody String id) {
		// 参数验证
		if (id == null||"".equals(id)) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		// 删除
		int rlt=resourceManagementServiceImpl.deleteEntityById(id);
		if (rlt == 1) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * 跳转到查看所有映射页面
	 */
	@RequestMapping(value="/viewAllMap_sitemesh_popup",method=RequestMethod.GET)
	public String viewAllMap(){
		return "/resourceManagement/viewAllMapPopup";
	}
	
	/**
	 * 获取所有资源映射
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "/queryUrlsList",method=RequestMethod.POST)
	@ResponseBody
	public List<ResourceEntity> queryUrlsList(HttpServletRequest request){
		//调用spring RequestMappingHandlerMapping类的方法，获取映射。
		Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
		//新建资源对象集合，用来接收spring扫描出来的所有映射。
		List<ResourceEntity> urls = new ArrayList<ResourceEntity>();
		//获取所有映射url。
		resourceManagementServiceImpl.mapList(handlerMethods, urls);
		//查看资源是否受保护。
		resourceManagementServiceImpl.checkProtectedRes(urls, request);
		return urls;
	}

	
}
